package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/ifnk/micro-blog/internal/blog/global"
	"github.com/ifnk/micro-blog/internal/pkg/base"
	response "github.com/ifnk/micro-blog/internal/pkg/http"
	"github.com/ifnk/micro-blog/protobuf/permission_pb"
	"github.com/infobloxopen/atlas-app-toolkit/query"
	"strconv"
	"strings"
)

func InitApiController(r *gin.RouterGroup) {
	api := r.Group("/api")
	// 这个 要 使用 jwt 和 casbin 中间件
	//api.Use(middleware.JWTAuth())
	api.GET("", getApis)
	api.GET("/:id", getApi)
	api.POST("", createApi)
	api.PUT("/:id", updateApi)
	api.DELETE("/:id", deleteApi)
}

func createApi(c *gin.Context) {
	var input permission_pb.Api
	_ = c.ShouldBindJSON(&input)

	res, err := global.ApiClient.Create(c, &permission_pb.CreateApiRequest{Payload: &input})
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	orm, err := res.GetResult().ToORM(c)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(orm, c)
}

func getApi(c *gin.Context) {
	idStr := c.Param("id")
	id, _ := strconv.Atoi(strings.TrimSpace(idStr))
	res, err := global.ApiClient.Read(c, &permission_pb.ReadApiRequest{Id: uint64(id)})
	orm, err := res.GetResult().ToORM(c)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(orm, c)
}
func deleteApi(c *gin.Context) {
	idStr := c.Param("id")
	id, _ := strconv.Atoi(strings.TrimSpace(idStr))
	res, err := global.ApiClient.Delete(c, &permission_pb.DeleteApiRequest{Id: uint64(id)})
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(res, c)
}
func updateApi(c *gin.Context) {
	var input permission_pb.ApiORM
	idStr := c.Param("id")
	id, _ := strconv.Atoi(strings.TrimSpace(idStr))
	_ = c.ShouldBindJSON(&input)
	pb, _ := input.ToPB(c)
	pb.Id = uint64(id)
	res, err := global.ApiClient.Update(c, &permission_pb.UpdateApiRequest{Payload: &pb})
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	orm, _ := res.GetResult().ToORM(c)
	response.OkWithData(orm, c)
}
func getApis(c *gin.Context) {
	var params base.PageEntity
	_ = c.ShouldBindQuery(&params)
	offset := int32(params.PageIndex-1) * int32(params.PageSize)
	limit := int32(params.PageSize)
	request := permission_pb.ListApiRequest{Paging: &query.Pagination{Offset: offset, Limit: limit}}
	apiRes, err := global.ApiClient.List(c, &request)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	global.Logger.Infof("apiRes -> %+v", apiRes)

	var pbResponse []permission_pb.ApiORM
	for _, item := range apiRes.GetResults() {
		orm, err := item.ToORM(c)
		if err != nil {
			response.FailWithMessage(err.Error(), c)
			return
		}
		pbResponse = append(pbResponse, orm)
	}
	var total int32
	if apiRes.PageInfo == nil {
		total = 0
	} else {
		total = apiRes.PageInfo.Size
	}

	response.OkWithData(ApiPageRes{
		Total: total,
		Items: pbResponse,
	}, c)
}

type ApiPageRes struct {
	Total int32
	Items []permission_pb.ApiORM
}
